package com.crayfish.quduo.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.crayfish.quduo.R;
import com.crayfish.quduo.model.JokeItemBean;
import com.crayfish.quduo.model.LaiFuDaoBean;
import com.crayfish.quduo.utils.ImageUtil;
import com.crayfish.quduo.utils.SystemUtil;
import com.crayfish.quduo.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by crayfish on 2016/12/28.
 */
public class JokeListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
            implements View.OnClickListener{

    private static final int VIEW_ITEM = 0;
    private static final int VIEW_PROG = 1;

    private Context mContext;
    private List<JokeItemBean.Item> datas;
    public FooterHolder footerHolder;

    public JokeListAdapter(Context context) {
        super();
        this.mContext = context;
        datas = new ArrayList<JokeItemBean.Item>();
    }

    public void addAll(List<JokeItemBean.Item> datas){
        this.datas.addAll(datas);
        notifyDataSetChanged();
    }

    public void clear(){
        this.datas.clear();
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == VIEW_ITEM) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_meipai_list,parent,false);
            NormalHolder vh = new NormalHolder(view);
            view.setOnClickListener(this);
            return vh;
        }else{
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.sample_common_list_footer,parent,false);
            footerHolder = new FooterHolder(view);
            return footerHolder;
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == datas.size()) {
            return VIEW_PROG;
        }else {
            return VIEW_ITEM;
        }
    }

    @Override
    public void onClick(View v) {
        if(onRecyclerViewItemClickListener != null){
            onRecyclerViewItemClickListener.onItemClick(v,(JokeItemBean.Item)v.getTag());
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof NormalHolder) {
            ImageUtil.imageBitmapLoad(mContext, datas.get(position).getImg(), ((NormalHolder)holder).iv_cover_pic);
            JokeItemBean.Item item = datas.get(position);
            ((NormalHolder)holder).txt_title.setText(item.getTitle());
            if (item.getComment() == 0) {
                ((NormalHolder)holder).txt_comment.setText("浏览量：1000");
            } else {
                ((NormalHolder)holder).txt_comment.setText("浏览量：" + item.getComment());
            }
            if (item.getLaud() == 0) {
                ((NormalHolder)holder).txt_laud.setText("·点赞数：500");
            } else {
                ((NormalHolder)holder).txt_laud.setText("·点赞数：" + +item.getLaud());
            }
            ((NormalHolder)holder).itemView.setTag(datas.get(position));
        }
    }

    @Override
    public int getItemCount() {
        return datas.size() + 1;
    }

    static class NormalHolder extends RecyclerView.ViewHolder{
        ImageView iv_cover_pic;
        TextView txt_title;
        TextView txt_comment;
        TextView txt_laud;
        public NormalHolder(View itemView) {
            super(itemView);
            iv_cover_pic = (ImageView)itemView.findViewById(R.id.iv_cover_pic);
            txt_title = (TextView)itemView.findViewById(R.id.txt_title);
            txt_comment = (TextView)itemView.findViewById(R.id.txt_comment);
            txt_laud = (TextView)itemView.findViewById(R.id.txt_laud);
        }
    }

    public OnRecyclerViewItemClickListener onRecyclerViewItemClickListener;

    public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener listener){
        this.onRecyclerViewItemClickListener = listener;
    }

    public interface OnRecyclerViewItemClickListener{
        void onItemClick(View v,JokeItemBean.Item item);
    }
}
